﻿using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmRegional : FrmFunctionBase
    {
        public FrmRegional()
        {
            InitializeComponent();
        }
        public static bool deleteReg(string id)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("did", id) });
            return (SystemInfo.DbData.IsOpen && (SystemInfo.DbData.ExecuteSql("delete MXHLY_SYS_REGIONAL where RGN_ID=:did", parms) == 1));
        }
        private void frmreg_AddToMain(object sender, EventArgs e)
        {
            string commandtext = "select * from MXHLY_SYS_REGIONAL WHERE RGN_CODE LIKE '" + SystemInfo.TownCode + "%'";
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, ref dt))
            {
                this.treeRegional.DataSource = dt;
            }
        }

        private void FrmRegional_Load(object sender, EventArgs e)
        {
            this.getTreeData();
        }

        private void FrmRegional_Shown(object sender, EventArgs e)
        {
            this.luProvince.EditValue = this.ProvinceData.Table.Select("RGN_CODE = '" + SystemInfo.ProvinceCode + "'")[0]["RGN_ID"];
            this.luCity.EditValue = this.CityData.Table.Select("RGN_CODE = '" + SystemInfo.CityCode + "'")[0]["RGN_ID"];
            string commandtext = "select RGN_ID,RGN_FULLNAME,RGN_NAME,RGN_CODE,RGN_LAYER,RGN_PARENT_ID,RGN_IS_LEAF,RGN_ACTIVE,RGN_CREATE_DATE,CREATE_ORG_CODE,INPUTCODE1,INPUTCODE2,'未管理' as RGN_MANAGE_STATE from MXHLY_SYS_REGIONAL WHERE RGN_CODE LIKE '" + SystemInfo.TownCode + "%'";
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, ref dt))
            {
                if ((dt != null) && (dt.Rows.Count > 0))
                {
                    string str2 = "select * from KTSYS_ORG_RGN";
                    DataTable table2 = new DataTable();
                    if ((SystemInfo.DbData.Retrieve(str2, ref table2) && (table2 != null)) && (table2.Rows.Count > 0))
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            if (table2.Select(string.Format(" RGN_CODE='{0}'", row["RGN_CODE"].ToString())).Length > 0)
                            {
                                row["RGN_MANAGE_STATE"] = "已管理";
                            }
                        }
                    }
                }
                this.treeRegional.DataSource = dt;
            }
        }

        private void getTreeData()
        {
            string commandtext = "select RGN_ID,RGN_FULLNAME,RGN_NAME,RGN_CODE,RGN_LAYER,RGN_PARENT_ID,RGN_IS_LEAF,RGN_ACTIVE,RGN_CREATE_DATE,CREATE_ORG_CODE,INPUTCODE1,INPUTCODE2,'未管理' as RGN_MANAGE_STATE from MXHLY_SYS_REGIONAL";
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, ref dt))
            {
                if (dt != null)
                {
                    int count = dt.Rows.Count;
                }
                this.TreeData = dt;
                this.luProvince.Properties.DataSource = this.ProvinceData = new DataView(dt) { RowFilter = "RGN_LAYER=1" };
                this.luCity.Properties.DataSource = this.CityData = new DataView(dt) { RowFilter = "1=2" };
            }
        }
        private void luProvince_EditValueChanged(object sender, EventArgs e)
        {
            this.luCity.EditValue = null;
            this.CityData.RowFilter = string.Format("RGN_PARENT_ID={0}", this.luProvince.EditValue);
        }

        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo) != DialogResult.No)
            {
                deleteReg(this.treeRegional.FocusedNode.GetDisplayText("RGN_ID").Trim());
                this.treeRegional.DeleteSelectedNodes();
            }
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            FrmRegionalSetup setup = new FrmRegionalSetup(this.treeRegional.FocusedNode.GetDisplayText("RGN_ID").Trim());
            setup.AddToMain += new FrmRegionalSetup.AddToMainEventHandler(this.frmreg_AddToMain);
            setup.ShowDialog();
        }

        private void nbiExit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.treeRegional.FocusedNode.GetValue("RGN_IS_LEAF").ToString() == "Y")
            {
                MessageBox.Show("该节点为末级节点，不能新增子节点");
            }
            else
            {
                string parentId = this.treeRegional.FocusedNode.GetValue("RGN_ID").ToString();
                string code = this.treeRegional.FocusedNode.GetValue("RGN_CODE").ToString();
                int num = Convert.ToInt32(this.treeRegional.FocusedNode.GetValue("RGN_LAYER"));
                if (num < 5)
                {
                    num++;
                }
                FrmRegionalSetup setup = new FrmRegionalSetup(parentId, code, num.ToString());
                setup.AddToMain += new FrmRegionalSetup.AddToMainEventHandler(this.frmreg_AddToMain);
                setup.ShowDialog();
            }
        }

        private void nbiOrganization_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string code = this.treeRegional.FocusedNode.GetDisplayText("RGN_CODE").Trim();
            new FrmRegion_Reg().Edit(code);
        }

        private void nbiPrint_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            //if (PrintHelper.IsPrintingAvailable)
            //{
            //    PrintHelper.ShowPreview(this.treeRegional);
            //}
        }

        private void sbQuery_Click(object sender, EventArgs e)
        {
            if (this.treeRegional.DataSource != this.TreeData)
            {
                if ((this.TreeData != null) && (this.TreeData.Rows.Count > 0))
                {
                    string commandtext = "select * from KTSYS_ORG_RGN";
                    DataTable dt = new DataTable();
                    if ((SystemInfo.DbData.Retrieve(commandtext, ref dt) && (dt != null)) && (dt.Rows.Count > 0))
                    {
                        foreach (DataRow row in this.TreeData.Rows)
                        {
                            if (dt.Select(string.Format(" RGN_CODE='{0}'", row["RGN_CODE"].ToString())).Length > 0)
                            {
                                row["RGN_MANAGE_STATE"] = "已管理";
                            }
                        }
                    }
                }
                this.treeRegional.DataSource = this.TreeData;
            }
            object editValue = this.luProvince.EditValue;
            object obj3 = this.luCity.EditValue;
            TreeListNode node = null;
            foreach (TreeListNode node2 in this.treeRegional.Nodes)
            {
                if (node2.Visible = node2.GetValue("RGN_ID").Equals(editValue))
                {
                    node = node2;
                }
            }
            foreach (TreeListNode node3 in node.Nodes)
            {
                node3.Visible = (obj3 == null) || node3.GetValue("RGN_ID").Equals(obj3);
            }
        }

        private void ShowHitInfo(TreeListHitInfo hi)
        {
            if (hi.Node != null)
            {
                this.checkNode = hi.Node.GetValue("RGN_ID").ToString();
            }
        }

        private void treeRegional_DoubleClick(object sender, EventArgs e)
        {
            FrmRegionalSetup setup = new FrmRegionalSetup(this.checkNode);
            setup.AddToMain += new FrmRegionalSetup.AddToMainEventHandler(this.frmreg_AddToMain);
            setup.ShowDialog();
        }

        private void treeRegional_GetNodeDisplayValue(object sender, GetNodeDisplayValueEventArgs e)
        {
            if (e.Column.FieldName == "RGN_LAYER")
            {
                e.Value = this.layerData[Convert.ToInt32(e.Value) - 1];
            }
            if (e.Column.FieldName == "RGN_IS_LEAF")
            {
                e.Value = (e.Value.ToString() == "N") ? "否" : "是";
            }
            if (e.Column.FieldName == "RGN_ACTIVE")
            {
                e.Value = (e.Value.ToString() == "N") ? "停用" : "可用";
            }
        }

        private void treeRegional_MouseDown(object sender, MouseEventArgs e)
        {
            this.ShowHitInfo(this.treeRegional.CalcHitInfo(new Point(e.X, e.Y)));
        }
    }
}
